package com.example.earthed.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.earthed.entity.User;
import com.example.earthed.model.Result;
import com.example.earthed.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping(path = "/app/user")
public class UserController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/sign")
    public Result<String> sign(@RequestBody User user){
        System.out.println(LocalDateTime.now());
        log.info("用户注册，信息：{}",user.toString());
        //md5加密
        user.setPwd(DigestUtils.md5DigestAsHex(user.getPwd().getBytes()));
        userService.save(user);
        System.out.println(LocalDateTime.now());
        return Result.success("用户注册成功");
    }

    /**
     * 用户登录
     * @param request
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<User> login(HttpServletRequest request, @RequestBody User user){

        String password = user.getPwd();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,user.getUserName());
        User theUser = userService.getOne(queryWrapper);

        if(theUser == null){
            return Result.error("账号或密码错误");
        }

        if(!theUser.getPwd().equals(password)){
            return Result.error("账号或密码错误");
        }

        if(theUser.getStatus() == 0){
            return Result.error("该账号已被禁用");
        }

        request.getSession().setAttribute("user",user.getId());
        return Result.success(user);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return Result.success("退出成功");
    }

    /**
     * 个人信息
     * @param request
     * @return
     */
    @GetMapping("/person/information")
    public Result<User> information(HttpServletRequest request){
        Long userId = (Long) request.getSession().getAttribute("user");
        return Result.success(userService.getById(userId));
    }

    /**
     * 修改个人信息
     * @param request
     * @param user
     * @return
     */
    @PutMapping("/person/information")
    public Result<String> updateInformation(HttpServletRequest request,@RequestBody User user){
        Long userId = (Long) request.getSession().getAttribute("user");
        user.setId(userId);
        userService.updateById(user);

        return Result.success("修改个人信息成功");
    }

    @PostConstruct
    public void init() {
        LOG.info("UserControl 启动啦");
        LOG.info("userService 注入啦");
    }

}

